﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CellsLifeActivity
{
    class Program
    {
        static bool gameFlag = false;

        static void Main()
        {
            Console.SetWindowSize(60, 40);
            Console.Title = "Homework #3: CellsLifeActivity";
            Program.gameFlag = false;

            PrintMainMenu();
        }

        public bool GameFlag
        {
            get
            {
                return gameFlag;
            }
            set
            {
                gameFlag=value;
            }
        }

        public static void PrintMainMenu()
        {
            Console.Clear();
            Console.SetCursorPosition(0, 5);
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write("\t\tCells' Life Activity");

            Console.SetCursorPosition(0,10);
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.Write("\t\tNew Game            ");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("---Press <N>");
            Console.WriteLine("");

            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.Write("\t\tSettings            ");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("---Press <S>");
            Console.WriteLine("");

            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.Write("\t\tAbout               ");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("---Press <A>");
            Console.WriteLine("");

            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.Write("\t\tExit                ");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("---Press <Escape>");

            if (Program.gameFlag == true)
            {
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.SetCursorPosition(0, 30);
                Console.WriteLine("\t\t\tPress <C> to continue...");
            }

            Console.ForegroundColor = ConsoleColor.White;
            //Console.ReadLine();

            ConsoleKeyInfo cki1;
            cki1 = Console.ReadKey();

           
            switch (cki1.Key)
            {
                case ConsoleKey.N:
                    Program.gameFlag = true;
                    GameLogic.GlobalFlag = true;
                    GameLogic.NewGame();
                    break;
                case ConsoleKey.S:
                    GameSettings();
                    break;
                case ConsoleKey.A:
                    AboutGame();
                    break;
                case ConsoleKey.Escape:
                    Console.Write("");
                    break;
                case ConsoleKey.C:
                    GameLogic.Game();
                    GameLogic.GlobalFlag = false;
                    GameLogic.NewGame();
                    break;
                default:
                    Console.Read();
                    break;
            }
            
        }

        static void AboutGame()
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("\t\tCells' Life Activity");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("");
            Console.WriteLine("\tThis console application represents life activity");
            Console.WriteLine("simulation of primitiv living creatures (cells).");
            Console.WriteLine("");
            Console.WriteLine("\tCells live in matrix 30x30. The initial density of");
            Console.WriteLine("matrix infill you can change in menu Settings. There");
            Console.WriteLine("are 3 kinds of matrix cells:");
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.WriteLine("\t\t\t\t' ' - empty cell,");
            Console.WriteLine("\t\t\t\t'*' - common cell,");
            Console.WriteLine("\t\t\t\t'x' - predatory cell.");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("\tOn <Spasebar> down the one time segment is walked.");
            Console.WriteLine("Over this period every cell makes the one move.");
            Console.WriteLine("");
            Console.WriteLine("Possible movement:");
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("  1. To give birth to new cell.");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Empty cell becomes common(predatory) if it has X common");
            Console.WriteLine("(predatory) neighbours. If both condition is fulfilled");
            Console.WriteLine("then new cell hase a type of dominated neighbours.");
            Console.WriteLine("User can change the value of X in menu Settings.");
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("  2. To die alone.");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("If common(predatory) cell has less the Y common(predatory)");
            Console.WriteLine("neighbours, then the current cell will die (becomes empty.)");
            Console.WriteLine("User can change the value of Y in menu Settings.");
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("  3. To die of overpopulation.");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("If common(predatory) cell has less the Z empty neighbours,");
            Console.WriteLine("then the current cell will die (becomes empty.)");
            Console.WriteLine("User can change the value of Z in menu Settings.");
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("  4. To eat a single 'common' neighbour by predatory cell.");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("If among the neighbours of predatory cell only the one");
            Console.WriteLine("common cell exists, the predatory cell will eat this");
            Console.WriteLine("common cell (will make it empty)");
            Console.WriteLine("");

            Console.WriteLine("Press <B> to back to MainMenu");
            ConsoleKeyInfo cki = Console.ReadKey();
            if (cki.Key == ConsoleKey.B) PrintMainMenu();
        }

        static void GameSettings()
        {
            string iniName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6)
                             + "\\cellsSettings.ini";                  //"E:\\cellsSettings.ini";
            cIni oIni = new cIni(iniName);
            
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("Settings:");
            Console.WriteLine("");
            Console.WriteLine("   Density of matrix infill            ---Press <D>");
            Console.WriteLine("   Empty cell                          ---Press <E>");
            Console.WriteLine("   Common cell                         ---Press <C>");
            Console.WriteLine("   Predatory cell                      ---Press <P>");
            Console.WriteLine("   Default settings                    ---Press <T>");

            Console.WriteLine("");
            Console.WriteLine("Press <B> to back to MainMenu");

            ConsoleKeyInfo cki;
            do {
                cki = Console.ReadKey();
            switch (cki.Key)
            {
                case ConsoleKey.B:
                    PrintMainMenu();
                    break;
                case ConsoleKey.D:
                    Console.WriteLine("");
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine("Density of matrix infill ->");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("   Enter the percentage of common and predatory cells");
                    Console.WriteLine("   in matrix. Else cells will be empty (Example: enter 33");
                        Console.Write("   press <Enter> enter 32 press <Enter>).  ");
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    Console.WriteLine("Then press <S>");
                    Console.WriteLine("   to save it");
                    Console.ForegroundColor = ConsoleColor.White;
                    string stC = Console.ReadLine();
                    string stP = Console.ReadLine();
                    do
                    {
                        cki = Console.ReadKey();
                        if (cki.Key == ConsoleKey.S)
                        {
                            //Set value in INI
                            oIni.WriteValue("Cells percentage", "CommonCellsPercent", stC);
                            oIni.WriteValue("Cells percentage", "PredatoryCellsPercent", stP);
                            GameSettings();
                        }
                    } while (cki.Key != ConsoleKey.S);
                    break;
                case ConsoleKey.E:
                    Console.WriteLine("");
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine("Empty cell ->");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("  1. Enter the enough number of 'common' neighbours");
                    Console.WriteLine("     it should have in order to make new 'common' one:  ");
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    Console.WriteLine("     Press <1> to set this parameter and then press <Enter>");
                    Console.WriteLine("     -> <S> to save it");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("  2. Enter the enough number of 'predatory' neighbours");
                    Console.WriteLine("     it should have in order to make new 'predatory' one:  ");
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    Console.WriteLine("     Press <2> to set this parameter and then press <Enter>");
                    Console.WriteLine("     -> <S> to save it");
                    Console.ForegroundColor = ConsoleColor.White;
                    do
                    {
                        cki = Console.ReadKey();
                        string str;
                        switch (cki.Key)
                        {
                            case ConsoleKey.D1:
                                ConsoleKeyInfo cki1;
                                Console.WriteLine("<1> ");
                                str = Console.ReadLine();
                                do
                                {
                                    cki1 = Console.ReadKey();
                                    if (cki1.Key == ConsoleKey.S)
                                    {
                                        //Set value in ini
                                        oIni.WriteValue("Empty Cell", "EnoughNumberOfCommonNeighbours", str);
                                        GameSettings();
                                        break;
                                    }
                                } while (cki1.Key != ConsoleKey.S);
                            
                                break;
                            case ConsoleKey.D2:
                                Console.WriteLine("<2>");
                                str = Console.ReadLine();
                                do
                                {
                                    cki1 = Console.ReadKey();
                                    if (cki1.Key == ConsoleKey.S)
                                    {
                                        //Set value in ini
                                        oIni.WriteValue("Empty Cell", "EnoughNumberOfPredatoryNeighbours", str);
                                        GameSettings();
                                        break;
                                    }
                                } while (cki1.Key != ConsoleKey.S);

                                break;
                            default:
                                break;
                        }


                    } while ((cki.Key != ConsoleKey.NumPad1) || (cki.Key != ConsoleKey.NumPad2));
                    break;
                case ConsoleKey.C:
                    Console.WriteLine("");
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine("Common cell ->");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("  1. Enter the enough number of 'common' neighbours");
                    Console.WriteLine("     it should have in order to doesn't die");
                        Console.Write("     from solitude:  ");
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                                         Console.WriteLine("Press <1> to set this parameter");
                                         Console.WriteLine("     and then press <Enter> -> <S> to save it");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("  2. Enter the enough number of 'empty' neighbours");
                    Console.WriteLine("     it should have in order to doesn't die");
                        Console.Write("     from overpopulation:  ");
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                                         Console.WriteLine("Press <2> to set this");
                                         Console.WriteLine("     parameter and then press <Enter> -> <S> to save it");
                    Console.ForegroundColor = ConsoleColor.White;
                    do
                    {
                        cki = Console.ReadKey();
                        string str;
                        if (cki.Key == ConsoleKey.D1)
                        {
                            ConsoleKeyInfo cki1;
                            Console.WriteLine("<1> ");
                            str = Console.ReadLine();
                            do
                            {
                                cki1 = Console.ReadKey();
                                if (cki1.Key == ConsoleKey.S)
                                {
                                    //Set value in ini
                                    oIni.WriteValue("Common Cell", "NumberOfEmptyNeighbours", str);
                                    GameSettings();
                                }
                            } while (cki.Key != ConsoleKey.S);

                        }
                        if (cki.Key == ConsoleKey.D2)
                        {
                            ConsoleKeyInfo cki1;
                            Console.WriteLine("<2>");
                            str = Console.ReadLine();
                            do
                            {
                                cki1 = Console.ReadKey();
                                if (cki1.Key == ConsoleKey.S)
                                {
                                    //Set value in ini
                                    oIni.WriteValue("Common Cell", "NumberOfSameKindNeighbours", str);
                                    GameSettings();
                                }
                            } while (cki.Key != ConsoleKey.S);
                        }
                    } while ((cki.Key != ConsoleKey.NumPad1) || (cki.Key != ConsoleKey.NumPad2));
                    break;
                case ConsoleKey.P:
                    Console.WriteLine("");
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine("Predatory cell ->");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("  1. Enter the enough number of 'predatory' neighbours");
                    Console.WriteLine("     it should have in order to doesn't die");
                        Console.Write("     from solitude:  ");
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                                         Console.WriteLine("Press <1> to set this parameter");
                                         Console.WriteLine("     and then press <Enter> -> <S> to save it");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("  2. Enter the enough number of 'empty' neighbours");
                    Console.WriteLine("     it should have in order to doesn't die");
                        Console.Write("     from overpopulation:  ");
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                                         Console.WriteLine("Press <2> to set this");
                                         Console.WriteLine("     parameter and then press <Enter> -> <S> to save it");
                    Console.ForegroundColor = ConsoleColor.White;
                    do
                    {
                        cki = Console.ReadKey();
                        string str;
                        if (cki.Key == ConsoleKey.D1)
                        {
                            ConsoleKeyInfo cki1;
                            Console.WriteLine("<1> ");
                            str = Console.ReadLine();
                            do
                            {
                                cki1 = Console.ReadKey(); 
                                if (cki1.Key == ConsoleKey.S)
                                {
                                    //Set value in ini
                                    oIni.WriteValue("Predatory Cell", "NumberOfEmptyNeighbours", str);
                                    GameSettings();
                                }
                            } while (cki1.Key != ConsoleKey.S);
                            
                        }
                        if (cki.Key == ConsoleKey.D2)
                        {
                            ConsoleKeyInfo cki1;
                            Console.WriteLine("<2>");
                            str = Console.ReadLine();
                            do
                            {
                                cki1 = Console.ReadKey();
                                if (cki1.Key == ConsoleKey.S)
                                {
                                    //Set value in ini
                                    oIni.WriteValue("Predatory Cell", "NumberOfSameKindNeighbours", str);
                                    GameSettings();
                                }
                            } while (cki1.Key != ConsoleKey.S);
                        }
                    } while ((cki.Key != ConsoleKey.NumPad1) || (cki.Key != ConsoleKey.NumPad2));
                    break;
                case ConsoleKey.T:
                    Console.WriteLine("");
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine("Default settings ->");
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("   Cells percentage");
                    Console.WriteLine("\tCommonCellsPercent=34");
                    Console.WriteLine("\tPredatoryCellsPercent=33");
                    Console.WriteLine("   Empty Cell");
                    Console.WriteLine("\tEnoughNumberOfCommonNeighbours=0");
                    Console.WriteLine("\tEnoughNumberOfPredatoryNeighbours=0");
                    Console.WriteLine("   Common Cell");
                    Console.WriteLine("\tNumberOfEmptyNeighbours=1");
                    Console.WriteLine("\tNumberOfSameKindNeighbours=4");
                    Console.WriteLine("   Predatory Cell");
                    Console.WriteLine("\tNumberOfEmptyNeighbours=5");
                    Console.WriteLine("\tNumberOfSameKindNeighbours=2");
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    Console.WriteLine("   Press <S> to save it");
                    Console.ForegroundColor = ConsoleColor.White;
                       do
                       {
                           cki = Console.ReadKey();
                           if (cki.Key==ConsoleKey.S)
                            {
                                //Set value in INI
                                oIni.WriteValue("Cells percentage", "CommonCellsPercent", "34");
                                oIni.WriteValue("Cells percentage", "PredatoryCellsPercent", "33");
                                oIni.WriteValue("Empty Cell", "EnoughNumberOfCommonNeighbours", "0");
                                oIni.WriteValue("Empty Cell", "EnoughNumberOfPredatoryNeighbours", "0");
                                oIni.WriteValue("Common Cell", "NumberOfEmptyNeighbours", "1");
                                oIni.WriteValue("Common Cell", "NumberOfSameKindNeighbours", "4");
                                oIni.WriteValue("Predatory Cell", "NumberOfEmptyNeighbours", "6");
                                oIni.WriteValue("Predatory Cell", "NumberOfSameKindNeighbours", "3");
                                GameSettings();
                            }
                       } while (cki.Key != ConsoleKey.S);
                    break;
                default:
                    //Console.Read();
                    break;
            }
            }   while(cki.Key!=ConsoleKey.B);
        }

    }
}
